import DBConnection from '../service/unit/mysql';
import {Development, Product} from "../service/unit/mysql/config";
import {create_article, updateArticleTitle} from "../controller";
import {deleteAnthology} from "../controller/delete/deleteAnthology";
import {deleteArticle} from "../controller/delete/article";
import {updateAnthology} from "../controller/update/updateAnthology";
import {readArticleFile} from "../controller/get/articleContent";
import getArticleAll from "../controller/get/getArticleAll";
import updateArticleContent from "../controller/update/updateArticleContent";
import createAnthology from "../controller/create/anthology";
import login from "../controller/query/login";
import register from "../controller/insert/register";
import updateUserAccount from "../controller/update/updateUserPassword";

const conn = new DBConnection({DBConfig:Product});

export default function webRouter(router) {

    router.post('/register',async (ctx)=>{
        const {account, password} = ctx.request.body;
        ctx.body = {
            status:200,
            result:await register(conn, {name:account, password:password})
        }
    });

    //登陆
    router.post('/login',async (ctx)=>{
        const {account, password} = ctx.request.body;
        ctx.body = {
            status:200,
            result:await login(conn, {user:account, password:password})
        }
    });

    //更新用户密码 未测试
    router.post('/update/user/password',async (ctx)=>{
        const {account, password} =  ctx.request.body;

        ctx.body = {
            statue: 200,
            result:await updateUserAccount(conn,{name:account, password})
        }
    });

    //更新文章内容
    router.post('/update/article/content',async (ctx)=>{
        const {id, title, content} =  ctx.request.body;

        ctx.body = {
            statue: 200,
            result: await updateArticleContent(conn, {id, content, title})
        }
    });
    //更新文章名
    router.post('/update/article/title',async (ctx)=>{
        const {title, storeId} = ctx.request.body;
        ctx.body = {
            status: 200,
            result: await updateArticleTitle(conn , {title}, {storeId:Number(storeId)}).
            then(()=>{
                return 'ok'
            })
        }
    });
    //更新文件名
    router.post('/update/anthology',async(ctx)=>{
        const {oldAnthology, newAnthology, author} = ctx.request.body;
        ctx.body = {
            status:200,
            result: await updateAnthology(conn, oldAnthology, newAnthology, author)
        }
    });
    //创建文集
    router.post('/create/anthology',async (ctx)=>{
        const {anthology, name} =  ctx.request.body;
        ctx.body = {
            status: 200,
            result: await createAnthology(conn, {anthology, author:name})
        }
    });
    //创建文章
    router.post('/create/article',async (ctx)=>{
        const {anthology, author, title} =  ctx.request.body;

        ctx.body = {
            status: 200,
            result: await create_article(
                conn,
                {anthology, author, title})
        };
    });

    //获取所有文章的列表
    router.post('/get/articleAll', async (ctx)=>{
        const {author, anthology} = ctx.request.body;

        ctx.body= {
            status:200,
            result:await getArticleAll(conn, anthology, author)
        }
    });

    //获取指定文章id的内容
    router.post('/get/article/content', async (ctx)=>{
        const {id} = ctx.request.body;
        const result = await readArticleFile(conn,id);
        ctx.body = {
            status:200,
            result:result.content
        }
    });
    //获取指定文章id的内容
    router.param('id',(id,ctx,next)=>{
        ctx.id = id;
        return next();
    }).get('/get/article/:id', async (ctx)=>{
        const {id} = ctx;
        ctx.body = {
            status:200,
            result: await readArticleFile(conn, id)
        }
    });


    //获取整个文集列表
    router.post('/get/anthology', async (ctx)=>{
        const COLUMN= ['id', 'anthology','author'];
        ctx.body = {
            status: 200,
            result: await conn.Query.query('anthology', COLUMN)
        };
    });
    //删除文章
    router.post('/delete/article',async (ctx)=>{
        const {storeId} = ctx.request.body;
        const TABLE_NAME = 'article';
        ctx.body ={
            statue: 200,
            results: await deleteArticle(conn, TABLE_NAME, {storeId})
        }
    });
    //删除文集
    router.post('/delete/anthology',async (ctx)=>{
        const {storeId, author} = ctx.request.body;
        const TABLE_NAME = 'anthology';
        ctx.body = {
            status: 200,
            result: await deleteAnthology(conn, TABLE_NAME,{storeId, author})
        }
    })
}